<?php
require_once APP . '/application/controllers/FrontAction.php';
require_once APP . '/application/models/ArticleModel.php';

/**
 * 新闻类型
 */
class NewsController extends FrontAction
{
    private $arr_stypes;
    
	public function init()
	{
		global $cfg;
    	$this->arr_stypes = array();
    	$this->arr_stypes[$cfg['listtype']['tpxw']] = _("index_tpxw");
    	$this->arr_stypes[$cfg['listtype']['tzgg']] = _("index_tzgg");
	}
	
	
    /**
     * 首页
     *
     */
    public function indexAction()
    {
    }
    

    public function listAction()
    {
    	global $cfg;
    	$nowp = intval($this->getParam("nowp", 1));
    	$cid = (int)$this->getParam("cid", 0);
    	$type = (int)$this->getParam("type");
    	
    	
    	$obj_list = new Alllist($this->db);
    	$obj_article = new ArticleModel($this->db);
    	
    	$view_file = "list";
    	$result = array();
    	
    	// 获取第一个分类
    	if (empty($cid) && !empty($type)){
    		$cid = $obj_list->getFirstListTypeId($type);
    	}    		
    		
    	if (empty($cid))
    		$this->redirectAction("index", "index");
    	
    	// 获取新闻中心分类
    	$arr_types = $obj_list->getAllListByType($this->cfg['listtype']['xwzx']);
    	$arr_xshd_types = $obj_list->getAllListByType($this->cfg['listtype']['xshd']);
    	$arr_ltrw_types = $obj_list->getAllListByType($this->cfg['listtype']['ltrw']);
    	$arr_gylt_types = $obj_list->getAllListByType($this->cfg['listtype']['gylt']);
    	if (isset($arr_types[$cid])){
    		$this->view->types = $arr_types;
    		$this->view->typename = $arr_types[$cid]['name'];
    		
    		$this->view->type = $this->cfg['listtype']['xwzx'];
    		$this->view->onetypename = _("list_xwzx");
    		$this->view->onetypeurl = $this->url($this->baseUrl, "", "", "type=" . $this->view->type);
    		
    		$result = $obj_article->fetchAll($cid, $nowp, 6);
    		
    		if ($cid == 2){// 专题报道
    			foreach ($result['list'] as $key => $v){
    				$result['list'][$key]['title'] = '[' . $v['typename'] . '] ' . $v['title'];
    			}
    		}
    		
    		$view_file = "list_xwzx";
    		$this->view->hmenu = "xwzx";
    	}else if (isset($this->arr_stypes[$cid])){// 图片新闻
			$this->view->typename = $this->arr_stypes[$cid];
			$view_file = "list_textnews";
			$result = $obj_article->fetchAll($cid, $nowp, 10);
		}else if (isset($arr_xshd_types[$cid])){// 学术活动
			$this->view->types = $arr_xshd_types;
    		$this->view->typename = $arr_xshd_types[$cid]['name'];
    		
    		$this->view->type = $this->cfg['listtype']['xshd'];
    		$this->view->onetypename = _("index_xshd");
    		$this->view->onetypeurl = $this->url($this->baseUrl, "", "", "type=" . $this->view->type);
    		
    		$result = $obj_article->fetchAll($cid, $nowp, 10);
			$view_file = "list_textnews";
    		$this->view->hmenu = "xshd";
		}else if (isset($arr_ltrw_types[$cid])){// 论坛人物
			$this->view->types = $arr_ltrw_types;
    		$this->view->typename = $arr_ltrw_types[$cid]['name'];
    		
    		$this->view->type = $this->cfg['listtype']['ltrw'];
    		$this->view->onetypename = _("index_ltrw");
    		$this->view->onetypeurl = $this->url($this->baseUrl, "", "", "type=" . $this->view->type);
    		
    		$result = $obj_article->fetchAll($cid, $nowp, 10);
			$view_file = "list_ltrw";			
    		$this->view->hmenu = "ltrw";
		}else if (isset($arr_gylt_types[$cid])){// 关于论坛
			$this->view->types = $arr_gylt_types;
    		$this->view->typename = $arr_gylt_types[$cid]['name'];
    		
    		$this->view->type = $this->cfg['listtype']['gylt'];
    		$this->view->onetypename = _("index_gylt");
    		$this->view->onetypeurl = $this->url($this->baseUrl, "", "", "type=" . $this->view->type);
    		
    		if ($arr_gylt_types[$cid]['isleaf']==1 && !empty($arr_gylt_types[$cid]['leafid'])){
    			$url = $this->url($this->baseUrl, "news", "detail", "aid=" . $arr_gylt_types[$cid]['leafid']);
    			$this->_redirect($url);
    		}
    		$result = $obj_article->fetchAll($cid, $nowp, 10);
			$view_file = "list_textnews";	
    		$this->view->hmenu = "gylt";
		}
		if ($cid == $cfg['listtype']['tzgg'])
			$this->view->hmenu = 'tzgg';

    	if (!empty($result))
    	{
	    	$this->view->list = $result['list'];
	    	$this->view->page = $result['page'];
	    	$this->view->pagenum = Util::pageNumStyle($result['page']);
    	}
    	$this->view->cid = $cid;
    	$this->render($view_file);
    }
    
    
    public function detailAction()
    {
    	global $cfg;
		$aid = (int)$this->getParam("aid");
		
		$obj_list = new Alllist($this->db);
		$obj_article = new ArticleModel($this->db);
		
		$content = $obj_article->show($aid);
		if (empty($content)){
			$this->redirectAction("index", "index");
		}
		
		$cid = $content['cid'];
		$viewfile = 'detail';
		
		// 获取新闻中心分类
		$arr_types = $obj_list->getAllListByType($this->cfg['listtype']['xwzx']);
    	$arr_xshd_types = $obj_list->getAllListByType($this->cfg['listtype']['xshd']);
    	$arr_ltrw_types = $obj_list->getAllListByType($this->cfg['listtype']['ltrw']);
    	$arr_gylt_types = $obj_list->getAllListByType($this->cfg['listtype']['gylt']);
		if (isset($arr_types[$cid])){
			$this->view->types = $arr_types;
			$this->view->typename = $arr_types[$cid]['name'];
		
			$this->view->type = $this->cfg['listtype']['xwzx'];
			$this->view->onetypename = _("list_xwzx");
			$this->view->onetypeurl = $this->url($this->baseUrl, "", "list", "type=" . $this->view->type);			
			
    		$this->view->pageprenext = $obj_article->getPreNext($this->baseUrl, $aid, $cid);
    		$this->view->relations = $obj_article->getRelations($this->baseUrl, $content);
    		$this->view->hmenu = "xwzx";
		}else if (isset($this->arr_stypes[$cid])){
			$this->view->typename = $this->arr_stypes[$cid];
		}else if (isset($arr_xshd_types[$cid])){// 学术活动
			$this->view->types = $arr_xshd_types;
    		$this->view->typename = $arr_xshd_types[$cid]['name'];
    		
    		$this->view->type = $this->cfg['listtype']['xshd'];
    		$this->view->onetypename = _("index_xshd");
    		$this->view->onetypeurl = $this->url($this->baseUrl, "", "list", "type=" . $this->view->type);
    		$this->view->hmenu = "xshd";
		}else if (isset($arr_ltrw_types[$cid])){// 论坛人物
			$this->view->types = $arr_ltrw_types;
    		$this->view->typename = $arr_ltrw_types[$cid]['name'];
    		
    		$this->view->type = $this->cfg['listtype']['ltrw'];
    		$this->view->onetypename = _("index_ltrw");
    		$this->view->onetypeurl = $this->url($this->baseUrl, "", "list", "type=" . $this->view->type);
    		$viewfile = "detail_ltrw";
    		$this->view->hmenu = "ltrw";
		}else if (isset($arr_gylt_types[$cid])){// 关于论坛
			$this->view->types = $arr_gylt_types;
    		$this->view->typename = $arr_gylt_types[$cid]['name'];
    		
    		$this->view->type = $this->cfg['listtype']['gylt'];
    		$this->view->onetypename = _("index_gylt");
    		$this->view->onetypeurl = $this->url($this->baseUrl, "", "list", "type=" . $this->view->type);
    		$this->view->hmenu = "gylt";
		}
		if ($cid == $cfg['listtype']['tzgg'])
			$this->view->hmenu = 'tzgg';
		
		$this->view->content = $content;
		$this->view->cid = $cid;
		$this->view->htitle = htmlspecialchars($content['title']);
    	$this->render($viewfile);
    }
    
}
